package com.example.simpleforum.dao;

import com.example.simpleforum.model.ForumAdminRelation;
import com.example.simpleforum.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ForumAdminRelationDao {
    
    /**
     * 添加板块管理员
     */
    public boolean addAdminRelation(ForumAdminRelation relation) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        boolean success = false;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO forum_admin_relation (forum_id, user_id, granted_by) VALUES (?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, relation.getForumId());
            pstmt.setInt(2, relation.getUserId());
            pstmt.setInt(3, relation.getGrantedById());
            
            int affectedRows = pstmt.executeUpdate();
            success = (affectedRows > 0);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, null);
        }
        
        return success;
    }
    
    /**
     * 检查用户是否是板块管理员
     */
    public boolean isForumAdmin(int forumId, int userId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        boolean isAdmin = false;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT 1 FROM forum_admin_relation WHERE forum_id = ? AND user_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, forumId);
            pstmt.setInt(2, userId);
            rs = pstmt.executeQuery();
            
            if (rs.next()) {
                isAdmin = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }
        
        return isAdmin;
    }
    
    /**
     * 获取用户管理的所有板块
     */
    public List<Map<String, Object>> findAdminForumsByUserId(int userId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Map<String, Object>> forums = new ArrayList<>();
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT far.forum_id, f.name " +
                         "FROM forum_admin_relation far " +
                         "JOIN forum f ON far.forum_id = f.id " +
                         "WHERE far.user_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userId);
            rs = pstmt.executeQuery();
            
            while (rs.next()) {
                Map<String, Object> forumInfo = new HashMap<>();
                forumInfo.put("id", rs.getInt("forum_id"));
                forumInfo.put("name", rs.getString("name"));
                forums.add(forumInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }
        
        return forums;
    }
    
    /**
     * 获取板块的所有管理员
     */
    public List<Integer> findAdminUsersByForumId(int forumId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Integer> userIds = new ArrayList<>();
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT user_id FROM forum_admin_relation WHERE forum_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, forumId);
            rs = pstmt.executeQuery();
            
            while (rs.next()) {
                userIds.add(rs.getInt("user_id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }
        
        return userIds;
    }
    
    /**
     * 移除板块管理员
     */
    public boolean removeAdminRelation(int forumId, int userId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        boolean success = false;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "DELETE FROM forum_admin_relation WHERE forum_id = ? AND user_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, forumId);
            pstmt.setInt(2, userId);
            
            int affectedRows = pstmt.executeUpdate();
            success = (affectedRows > 0);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, null);
        }
        
        return success;
    }
}